/*
 * @Description: 依赖的发布订阅模式
 * @Author: wangqi
 * @Date: 2021-05-24 23:08:10
 * @LastEditTime: 2021-05-25 22:56:57
 */
var uid = 0;
export default class Dep {
    constructor() {
        // 用来存储订阅者 [存放watcher实例]
        this.id = uid++;
        this.subs = [];
    }

    // 添加订阅
    addSub(sub) {
        this.subs.push(sub);
    }

    // 添加依赖
    depend() {
        if (Dep.target) {
            this.addSub(Dep.target);
        }
    }

    // 通知更新
    notify() {
        const subs = this.subs.slice();
        for (let i = 0, len = subs.length; i < len; i++) {
            subs[i].update();
        }
    }
}
